package org.open.cobertura.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class PathUtil {
    /**
     * 字节码的根路径
     */
    private static String classes="classes";
    private static String openCoberturaClasses="open-cobertura-classes";
    private static String openCoberturaReport="open-cobertura-report";
    /**
     * 获取类所在的字节码路径
     * @param className
     * @return
     */
    public static String getClassesPath(String className){
        // 获取Maven项目的根目录
        String baseDir = System.getProperty("user.dir");
        // 获取Maven构建输出目录
        String targetDir = System.getProperty("project.build.directory", "target");
        // 合并根目录和构建输出目录得到完整的target路径
        String targetPath = baseDir + System.getProperty("file.separator") + targetDir;
        String path=targetPath+"/"+classes+className;
        return path;
    }

    /**
     * 获取插桩后的字节码路径
     * @param className
     * @return
     */
    public static String getClassesOutPath(String className){
        // 获取Maven项目的根目录
        String baseDir = System.getProperty("user.dir");
        // 获取Maven构建输出目录
        String targetDir = System.getProperty("project.build.directory", "target");
        // 合并根目录和构建输出目录得到完整的target路径
        String targetPath = baseDir + System.getProperty("file.separator") + targetDir;
        String path=targetPath+"/"+openCoberturaClasses+className;
        createDir(path);
        return path;
    }

    /**
     * 得到报告输出路径
     * @return
     */
    public static String getReportOutPath(){
        // 获取Maven项目的根目录
        String baseDir = System.getProperty("user.dir");
        //String targetDir = System.getProperty("project.build.directory", "target");
        // 合并根目录和构建输出目录得到完整的target路径
        //String targetPath = baseDir + System.getProperty("file.separator") + targetDir;
        String path=baseDir+"/"+openCoberturaReport;
        createDir(path);
        return path;
    }

    public static String getTestResourcePath(String fileName){
        // 获取Maven项目的根目录
        String baseDir = System.getProperty("user.dir");
        // 获取Maven构建输出目录
        String targetDir = "src/test/resources";
        // 合并根目录和构建输出目录得到完整的target路径
        String targetPath = baseDir +"/"+ targetDir;
        return targetPath+fileName;
    }
    public static String getJavaResourcePath(String fileName){
        // 获取Maven项目的根目录
        String baseDir = System.getProperty("user.dir");
        String projectName="";
        String targetDir=System.getProperty("project.build.directory");
        // 获取Maven构建输出目录
        String reports = "/template";
        if(System.getProperty("project.build.directory")==null){
            projectName= PathUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath();
            return projectName+"/"+reports+"/"+fileName;
        }else{
            projectName=targetDir;
            // 合并根目录和构建输出目录得到完整的target路径
            String targetPath = baseDir +"/"+ projectName;
            return targetPath+"/"+reports+"/"+fileName;
        }
    }

    public static void copyClass(String originClass,String targetClassName){
        String originClassPath=getClassesPath(originClass);
        String targetClassPath=getClassesOutPath(targetClassName);
        File originFile=new File(originClassPath);
        File targetFile = new File(targetClassPath, originFile.getName());

        try (FileInputStream fileInputStream = new FileInputStream(originFile);
             FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
             FileChannel sourceChannel = fileInputStream.getChannel();
             FileChannel targetChannel = fileOutputStream.getChannel()) {

            targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
            System.out.println("文件复制成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copyResources(String fileName,String targetClassName){
        String originClassPath=getTestResourcePath(fileName);
        String targetClassPath=getClassesOutPath(targetClassName);
        File originFile=new File(originClassPath);
        File targetFile = new File(targetClassPath, originFile.getName());

        try (FileInputStream fileInputStream = new FileInputStream(originFile);
             FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
             FileChannel sourceChannel = fileInputStream.getChannel();
             FileChannel targetChannel = fileOutputStream.getChannel()) {

            targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
            System.out.println("文件资源复制成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建目录
     * @param path
     */
    private static void createDir(String path){
        File file=new File(path);
        File fileParent=new File(file.getParent());
        if(!fileParent.exists()){
            fileParent.mkdirs();
        }
    }
}
